<?php

function MINE_get_state($adress){
	global $config;
	
	
	$catch = SQL_select_one("catch", "url = '$adress'");
	if ($catch["date"]+$config["mine"]["cath"] >= time()){
		return array(
			"state" => $catch["state"],
			"name"	=> $catch["name"],
			"online"=> $catch["user"],
			"max"	=> $catch["max"]
		);
	}
	
	
	$server = explode(':',$adress);
	if (!isset($server[1]))
		$server[1] = $config["mine"]["port"];
	
	$fp = fsockopen($server[0], $server[1],$error,$errst,$config["mine"]["timeout"]);
	
	if (!$fp){
		SQL_insert_update('catch', array(
			"url"	=> $adress,
			"date"	=> time(),
			"state" => "Offline",
			"name"	=> "",
			"user"	=> 0,
			"max"	=> 0
			)
		);
		
		return array("state" => 'Offline', "error" => $errst);
	}else{
		$out["state"] = 'Online';
	}
	
	fputs($fp, Chr(254)); 								// State request
	
	$res = '';
	while(!feof($fp)) {
	     $res .= fgets($fp, 128);
	}
	fclose($fp);
	
	
	if (!(($res[0] == Chr(255)) AND ($res[0] == Chr(255)) AND ($res[0] == Chr(255)))){
		SQL_insert_update('catch', array(
			"url"	=> $adress,
			"date"	=> time(),
			"state" => "Offline",
			"name"	=> "",
			"user"	=> 0,
			"max"	=> 0
			)
		);
		return array("state" => 'Server Fehler');
	}

	for ($i = 4; $res[$i] <> Chr(167); $i += 2){
		$out["name"] .=  $res[$i];
	}
	
	for ($i += 2; $res[$i] <> Chr(167); $i += 2){
		$out["online"] .=  $res[$i];
	}
	
	for ($i += 2; isset($res[$i]); $i += 2){
		$out["max"] .=  $res[$i];
	}
	
	SQL_insert_update('catch', array(
			"url"	=> $adress,
			"date"	=> time(),
			"state" => $out["state"],
			"name"	=> $out["name"],
			"user"	=> $out["online"]+0,
			"max"	=> $out["max"]+0
		)
	);
	
	return $out;
}

function MINE_server_add_addmin($id,$admin){
	SQL_insert('server_has_admin',
		array( 
			"name" 	=> $admin,
			"id"	=> $id
		)
	);	
}

function MINE_server_is_addmin($id,$admin){
	return (SQL_select_one('server_has_admin',"name = '$admin' and id = $id") <> null);	
}

function MINE_server_detail($id){
	$out["server"] 	= SQL_select_one('server',"id = $id AND server.mode <> '0'");
	$out["admin"]	= SQL_select_as_array('server_has_admin',"id = $id");
	$out["kommentar"]= SQL_select_as_array('kommentar',"id = $id");
	return $out;
}

function MINE_server_list($admin = false){
	if ($admin){
		return SQL_select_as_array(
			'server, server_has_admin',
			"server.id = server_has_admin.id AND server_has_admin.name = '$admin' AND server.mode <> '0'",
			'server.name AS name, server.id AS id','name');
	}else{
		//return SQL_select_as_array('server, kommentar',false,'*','server.name');
		return SQL_query_as_array("SELECT * FROM server LEFT JOIN (SELECT AVG(vote) as vote, id AS VOTE_ID FROM kommentar GROUP BY id) AS VOTE ON server.id=VOTE.VOTE_ID WHERE server.mode = '1' ORDER BY server.name");
	}
}

function MINE_server_update($id, $set){
	global $_username;

	$data = $set;
	if ($id == false){
		unset($data["id"]);
		$id = SQL_insert('server', $data);
		MINE_server_add_addmin($id,$_username);
		return true;
	}else{
		if (!MINE_server_is_addmin($id, $_username))
			error("MINE_76","Keine berechtigung für diesen Server");		
		$data["id"] = $id;
		return SQL_insert_update('server', $data);
	}
		
}
